<?php

class C_pages extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->library('myclass');
        $this->myclass->not_admin();
    }

    public function add_pages() {
        $data = array();
        $this->load->model('M_categories');
        $data['categories'] = $this->M_categories->view_categories();

        //kiem tra ton tai post
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $errors = array();
            if (empty($_POST['page_name'])) {
                $errors[] = 'page name';
            } else {
                $page_name = $_POST['page_name'];
            }
            if (empty($_POST['content'])) {
                $errors[] = 'content';
            } else {
                $content = $_POST['content'];
            }
            if ($_POST['category'] == 'Select Category') {
                $errors[] = 'category';
            } else {
                $cat_name = $_POST['category'];
            }

            if (empty($errors)) {
                $this->load->model('M_pages');
                $data['pages'] = $this->M_pages->add_pages($page_name, $content, $cat_name);
                if ($data['pages'] == 1) {
                    $data['mg1'] = "add thanh cong";
                }
            } else {
                $data['mg1'] = "add xit";
                if (in_array('page name', $errors)) {
                    $data['mg2'] = "dien page name vao";
                }
                if (in_array('category', $errors)) {
                    $data['mg3'] = "dien category vao";
                }
                if (in_array('content', $errors)) {
                    $data['mg4'] = "dien content vao";
                }
            }
        }
        $this->load->view('v_add_pages', $data);
    }

    public function view_pages() {
        $this->load->model('M_pages');
        $data['pages'] = $this->M_pages->view_pages();
        $this->load->view('v_view_pages', $data);
        $this->output->cache(5);  
    }

    public function edit() {
        //in ra du lieu trong form de edit
        $this->load->model('M_pages');
        $dt = $data['page'] = $this->M_pages->view_page($_GET['pg']);
        //lay cat_name nho
        $this->load->model('M_categories');
        $dt9 = $data['cate9'] = $this->M_categories->view_category($dt[0]['cat_id']);
        //lay cat_name tong
        $this->load->model('M_categories');
        $dt2 = $data['categories'] = $this->M_categories->view_categories();
        // sau khi submit
        //kiem tra ton tai post
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $errors = array();
            //print_r($_POST);
            if (empty($_POST['page_name'])) {
                $errors[] = 'page name';
            } else {
                $page_name = $_POST['page_name'];
            }
            if (empty($_POST['content'])) {
                $errors[] = 'content';
            } else {
                $content = $_POST['content'];
            }
            if ($_POST['category'] == 'Select Category') {
                $errors[] = 'category';
            } else {
                $cat_name = $_POST['category'];
            }
           // print_r($errors);
            //echo $cat_name;
            //if()
            if (empty($errors)) {
                //lay cat_id
                $this->load->model('M_categories');
                $dt3 = $data['cate'] = $this->M_categories->view_category2($cat_name);
                //print_r($data['cate']);

                $cat_id = $dt3[0]['cat_id'];
                //
                //
                //echo $cat_id;
                $page_id = $_GET['pg'];
                $this->load->model('M_pages');
                $data['pages'] = $this->M_pages->update_page($page_id, $page_name, $content, $cat_id);
                if ($data['pages'] == 1) {
                    $data['mg1'] = "add thanh cong";
                }
            } else {
                $data['mg1'] = "add xit";
                if (in_array('page name', $errors)) {
                    $data['mg2'] = "dien page name vao";
                }
                if (in_array('category', $errors)) {
                    $data['mg3'] = "dien category vao";
                }
                if (in_array('content', $errors)) {
                    $data['mg4'] = "dien content vao";
                }
            }
        }
        //load lai du lieu hien thi trong form
        $this->load->model('M_pages');
        $dt = $data['page'] = $this->M_pages->view_page($_GET['pg']);
        // goi ra view
        $this->load->view('v_edit_pages', $data);
    }

    public function delete() {
        $data = array();
        if (isset($_GET['pg'])) {
            $this->load->model('M_pages');
            $data['flag'] = $this->M_pages->delete($_GET['pg']);
            $this->load->helper('url');
            redirect('c_pages/view_pages');
        }
    }
    
    public function test(){
        $this->load->model('M_pages');
        $data = $this->M_pages->test_data();
        echo $data;
    }
    
    
    
    
    
    

}

?>
